<template>
    <div>
        <div class="sale-type-box">
            <el-radio-group v-model="form.saleType" @change="changeSaleType">
                <div><el-radio-button value="正向抹零">正向抹零</el-radio-button></div>
            </el-radio-group>
        </div>
        <el-form class="form-box" ref="formRef" :model="form" label-width="auto" :rules="rules" size="large">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="抹零金：" prop="salaAmt">
                        <div class="salaAmt">{{ yhje }}</div>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="form.saleType == '正向抹零'">
                    <el-form-item label="抹零金额：" prop="salaAmt">
                        <el-input v-model="form.salaAmt" placeholder="请输入金额" @focus="focusForm('salaAmt')"
                            @blur="blurForm" oninput="value=value.replace(/[^0-9.]/g,'')"/>
                        <div class="rule">抹零金额限制：<span class="color-FF6A00">小于{{ max }}元</span></div>
                        <!-- <el-input-number class="input-number" v-model="form.salaAmt" :min="0" :max="10"  :controls="false" placeholder="请输入金额"
                            @focus="focusForm('salaAmt')" @blur="blurForm" style="width: 100%;"/> -->
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script setup >
import useConfigStore from '@/store/config'
const configStore = useConfigStore()
// 接收父组件参数 props
const props = defineProps({
    tableInfo: {
        type: Object,
        required: false,
    },
    selectWares: {
        type: Array,
        required: false,
    },
    saleData: {
        type: Array,
        required: false,
    },
    payData: {
        type: Array,
        required: false,
    },
    form: {
        type: Object,
        required: false,
    },
    receive: {
        type: String,
        required: false,
    },
    price: {
        type: String,
        required: false,
    },
    yhje: {
        type: String,
        required: false,
    },
    unpay: {
        type: String,
        required: false,
    },
})
const receive = toRef(props, 'receive')
const tableInfo = toRef(props, 'tableInfo')
const selectWares = toRef(props, 'selectWares')
const saleData = toRef(props, 'saleData')
const payData = toRef(props, 'payData')
const yhje = toRef(props, 'yhje')
const form = toRef(props, 'form')
const unpay = toRef(props, 'unpay')
// let { form, } = props;
const emit = defineEmits(['focusForm'])
// 数字键盘
const focusForm = (str) => {
    // keyboard.value = true
    emit('focusForm', str)
}
const blurForm = (params) => {
    // keyboard.value = false
}
// 切换form.saleType
const changeSaleType = () => {
    form.value.salaAmt = ''
    form.value.salaPercent = ''
    form.value.finalAmt = ''
}
// 获取抹零限制
let max = ref('')
const get_max = (params) => {
    max.value = '10'
}
get_max()
// 输入框验证 小于max
const validator_Mobile = (rule, value, callback) => {
    console.log('max.value :>> ', max.value);
    if (value <= Number(max.value)) {
        callback()
    } else {
        callback(new Error('The password can not be less than 6 digits'))
    }
}
const rules = reactive({
    // salaAmt: [
    //     { validator: validator_Mobile, message: `请输入小于 ${max.value} 的数`, trigger: 'change' },
    // ],
})
const handleKeyboardConfirm = async() => {
    if (Number(form.value.salaAmt) > Number(receive.value)  ) return ElNotification({
        title: '提示',
        message: '抹零金额不能大于应收金额',
        type: 'warning',
    })
    // if (Number(form.value.salaAmt) > Number(unpay.value)) return ElNotification({
    //     title: '提示',
    //     message: '抹零金额不能大于还需收款金额',
    //     type: 'warning',
    // })
    if (yhje.value > Number(max.value)) return ElNotification({
        title: '提示',
        message: '抹零金额大于' + max.value + '元',
        type: 'warning',
    })
    let flag = false
    saleData.value.forEach(item => {
        if (item.yhxm == form.value.actionType) {
            flag = true
        }
    });
    if (flag) return ElNotification({
        title: '提示',
        message: '已经存在抹零',
        type: 'warning',
    })
    const saleObj = {
        yhxm: form.value.actionType,
        je: yhje.value,
        code: tableInfo.value.payBatchCode,
        amt: yhje.value,
        type: form.value.actionType,
        subType: form.value.saleType,
    }
    saleData.value.push(saleObj)
    // console.log('saleData :>> ', saleData);
}
defineExpose({ handleKeyboardConfirm })

</script>

<style scoped lang="scss">
.sale-type-box {
    // width: 360px;
    // display: flex;
    // justify-content: space-between;
    padding-top: 20px;

    ::v-deep {
        .el-radio-group {
            display: flex;
            // justify-content: space-between;
        }

        .el-radio-button {
            margin-right: 12px;

            .el-radio-button__inner {
                width: 112px;
                height: 48px;
                line-height: 48px;
                background: #E6E6E6;
                border-radius: 8px;
                font-weight: 400;
                font-size: 16px;
                color: #666666;
                border: 0;
                padding: 0;
            }
        }

        .is-active {
            .el-radio-button__inner {
                background: #FFE566;
                color: #212121;
            }
        }
    }
}

.form-box {
    margin-top: 20px;

    ::v-deep {
        .el-input__wrapper {
            background: #F9F9F9;
            border-radius: 8px;
            border: 0;
            font-size: 16px;
            color: #999999;
        }
    }

    .salaAmt {
        font-weight: 400;
        font-size: 16px;
        color: #FF3333;
    }

}

.rule {
    font-size: 12px;
    color: #999999;
}

.color-FF6A00 {
    color: #FF6A00;
}
@import '@/styles/com_style_input.css';
</style>
